home *** CD-ROM | disk | FTP | other *** search
/ Mission 3 / Mission 3.zip / Mission 3.iso / texte / qed / src / set.c < prev    next >
C/C++ Source or Header  |  1998-10-19  |  4KB  |  241 lines

  1. #include "global.h"
  2. #include "set.h"
  3.  
  4. static char bits[] = {128, 64, 32, 16, 8, 4, 2, 1};
  5.  
  6. /*****************************************************************************/
  7.  
  8. int setfree(SET set)
  9. {
  10.     int    i;
  11.     unsigned long help, h;
  12.  
  13.     h = 0xFFFFFFFFL;
  14.     for (i = 0; i < SETMAX; i += 32)
  15.         if ((help = *set++) != h)
  16.         {
  17.             h = 0x80000000L;
  18.             while (help >= h)
  19.             {
  20.                 i++; 
  21.                 help <<= 1;
  22.             }
  23.             break;
  24.         }
  25.     return i;
  26. }
  27.  
  28. /*****************************************************************************/
  29.  
  30. int setmax(SET set)
  31. {
  32.     int    i;
  33.     unsigned long help;
  34.  
  35.     for (i = SETSIZE; (--i) >= 0; )
  36.         if ((help = set[i]) != 0L)
  37.         {
  38.             i *= 32;
  39.             for (i+=31; (help&1L)==0; i--,help>>=1) ;
  40.             return (i);
  41.         }
  42.     return -1;
  43. }
  44.  
  45. /*****************************************************************************/
  46.  
  47. int setmin(SET set)
  48. {
  49.     int    i;
  50.     unsigned long help, h;
  51.  
  52.     for (i=0; i<SETMAX; i+=32)
  53.         if ((help=*set++)!=0L)
  54.         {
  55.             h = 0x80000000L;
  56.             while (help<h)
  57.             {
  58.                 i++; help<<=1;
  59.             }
  60.             break;
  61.         }
  62.     return i;
  63. }
  64.  
  65. /*****************************************************************************/
  66.  
  67. void setcpy (SET set1, SET set2)
  68. {
  69.     memcpy(set1, set2, SETSIZE * (int) sizeof(unsigned long));
  70. }
  71.  
  72. /*****************************************************************************/
  73. #if (SETSIZE&1)!=0
  74.     So gehts nicht
  75. #endif
  76.  
  77. void setall (SET set)
  78. {
  79.     int i;
  80.  
  81.     i = SETSIZE/2;
  82.     while ((--i)>=0)
  83.     {
  84.         *set++ = 0xFFFFFFFFL;
  85.         *set++ = 0xFFFFFFFFL;
  86.     }
  87. }
  88.  
  89. /*****************************************************************************/
  90.  
  91. void setclr (SET set)
  92. {
  93.     int i;
  94.  
  95.     i = SETSIZE/2;
  96.     while ((--i)>=0)
  97.     {
  98.         *set++ = 0L;
  99.         *set++ = 0L;
  100.     }
  101. }
  102.  
  103. /*****************************************************************************/
  104.  
  105. void setnot (SET set)
  106. {
  107.     int i;
  108.  
  109.     i = SETSIZE/2;
  110.     while ((--i)>=0)
  111.     {
  112.         *set++ ^= 0xFFFFFFFFL;
  113.         *set++ ^= 0xFFFFFFFFL;
  114.     }
  115. }
  116.  
  117. /*****************************************************************************/
  118.  
  119. void setand (SET set1, SET set2)
  120. {
  121.     int i;
  122.  
  123.     i = SETSIZE/2;
  124.     while ((--i)>=0)
  125.     {
  126.         *set1++ &= *set2++;
  127.         *set1++ &= *set2++;
  128.     }
  129. }
  130.  
  131. /*****************************************************************************/
  132.  
  133. void setor (SET set1, SET set2)
  134. {
  135.     int i;
  136.  
  137.     i = SETSIZE/2;
  138.     while ((--i)>=0)
  139.     {
  140.         *set1++ |= *set2++;
  141.         *set1++ |= *set2++;
  142.     }
  143. }
  144.  
  145. /*****************************************************************************/
  146.  
  147. void setxor (SET set1, SET set2)
  148. {
  149.     int i;
  150.  
  151.     i = SETSIZE/2;
  152.     while ((--i)>=0)
  153.     {
  154.         *set1++ ^= *set2++;
  155.         *set1++ ^= *set2++;
  156.     }
  157. }
  158.  
  159. /*****************************************************************************/
  160.  
  161. void setincl (SET set, int elt)
  162. {
  163.     if (elt>=0 && elt<=SETMAX)
  164.         *((char*)set+(elt>>3)) |= bits[elt&7];
  165. }
  166.  
  167. /*****************************************************************************/
  168.  
  169. void setexcl (SET set, int elt)
  170. {
  171.     if (elt>=0 && elt<=SETMAX)
  172.         *((char*)set+(elt>>3)) &= ~bits[elt&7];
  173. }
  174.  
  175. /*****************************************************************************/
  176.  
  177. void setchg (SET set, int elt)
  178. {
  179.     if (elt>=0 && elt<=SETMAX)
  180.         *((char*)set+(elt>>3)) ^= bits[elt&7];
  181. }
  182.  
  183. /*****************************************************************************/
  184.  
  185. bool setin (SET set, int elt)
  186. {
  187.     if (elt>=0 && elt<=SETMAX)
  188.         return ((*((char*)set+(elt>>3)) & bits[elt&7]) ? TRUE : FALSE);
  189.     else
  190.         return (FALSE);
  191. }
  192.  
  193. /*****************************************************************************/
  194.  
  195. bool setcmp (SET set1, SET set2)
  196. {
  197.     int i;
  198.  
  199.     if (set2==NULL)
  200.         for (i=0; i<SETSIZE && *set1++==0; i++);
  201.     else
  202.         for (i=0; i<SETSIZE && *set1++==*set2++; i++);
  203.     return (i==SETSIZE);
  204.  
  205. /*****************************************************************************/
  206.  
  207. int setcard (SET set)
  208. {
  209.     int i, card, max;
  210.  
  211.     max = setmax(set);
  212.     for (i=setmin(set), card=0; i<=max; i++)
  213.         if (setin (set, i)) card++;
  214.  
  215.     return (card);
  216. }
  217.  
  218. /*****************************************************************************/
  219.  
  220. void str2set (char *str, SET set)
  221. {
  222.     int    i;
  223.  
  224.     setclr(set);
  225.     i = 0;
  226.     while(str[i])
  227.     {
  228.         if (str[i]=='-' && i>0)
  229.         {
  230.             char c;
  231.  
  232.             i++;
  233.             for (c=str[i-2]; c<str[i]; c++)
  234.                 setincl(set,c);
  235.         }
  236.         else
  237.             setincl(set,str[i++]);
  238.     }
  239. }
  240.